import axios from 'axios';
import * as constants from './constants';

const changeAllOrderData = (data) => ({
  type: constants.GET_ALLORDER,
  allOrderData: data.data,
  orderTotal: data.recordsTotal,
  totalIncome: data.otherData.totalIncome,
  balance: data.otherData.balance,
  totalCost: data.otherData.totalCost
});

const changeOrdering = (data) => ({
  type: constants.GET_ORDERING,
  orderingData: data
});

const changeOrdered = (data) => ({
  type: constants.GET_ORDERED,
  orderedData: data
});

// allOrderData: [],
// orderTotal: '',
// totalIncome: '',
// balance: '',
// totalCost: ''
export const getAllOrderData = () => {
  let userId = localStorage.getItem("id");
  return (dispatch) => {
    axios.get('http://localhost:9080/order/user?start=0&length=10&userId='+userId, {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      dispatch(changeAllOrderData(res.data));
    }).catch(() => {
      console.log('error');
    })
  }
}


// orderingData: [],
export const getOrderingData = () => {
  let userId = localStorage.getItem("id");
  return (dispatch) => {
    axios.get('http://localhost:9080/order/search?start=0&length=10&buyerId='+userId+'&status=0', {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      dispatch(changeOrdering(res.data.data));
    }).catch(() => {
      console.log('error');
    })
  }
}

// orderedData: [],
export const getOrderedData = () => {
  let userId = localStorage.getItem("id");
  return (dispatch) => {
    axios.get('http://localhost:9080/order/search?start=0&length=10&buyerId='+userId+'&status=1', {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      dispatch(changeOrdered(res.data.data));
    }).catch(() => {
      console.log('error');
    })
  }
}